/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.lucene;

import com.wallellen.core.ParameterService;
import com.wallellen.lucene.face.WordAnalyzerFace;
import com.wallellen.lucene.server.DocIndexImpl;
import com.wallellen.lucene.server.DocIndexInter;
import com.wallellen.lucene.server.DocQueryImpl;
import com.wallellen.lucene.server.DocQueryInter;
import com.wallellen.parameter.ParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class LuceneFace {
    private static final Logger log = LoggerFactory.getLogger(LuceneFace.class);

    private LuceneFace() {
    }

    private static LuceneFace obj;

    /**
     * 获得服务实例
     *
     * @return
     */
    public static LuceneFace inctance() {
        if (obj == null) {
            obj = new LuceneFace();
        }
        return obj;
    }

    /**
     * 分词服务
     *
     * @return
     */
    public WordAnalyzerFace getWordAnalyzerFace() {
        return new WordAnalyzerFace();
    }

    /**
     * 建立索引服务
     *
     * @param indexDir
     * @return
     * @throws java.io.IOException
     */
    public DocIndexInter getDocIndex(File indexDir) throws IOException {
        return DocIndexImpl.getInstance(indexDir);
    }

    /**
     * 查询索引服务
     *
     * @param indexFile
     * @return
     */
    public DocQueryInter getDocQuery(File indexDir) {
        return DocQueryImpl.getInstance(indexDir);
    }

    /**
     * 多索引查询服务
     *
     * @param indexFiles
     * @return
     */
    public DocQueryInter getDocQuery(List<File> indexDirs) {
        return DocQueryImpl.getInstance(indexDirs);
    }

    /**
     * 获得索引路径（luncene_index_dir+File.separator + path）
     *
     * @param path 基于所配置目录的相对路径
     * @return
     */
    public File getIndexPathFile(String path) {
        File indexDir = new File(getFileDirPath() + File.separator + path);
        return indexDir;
    }

    public static String getFileDirPath() {
        ParameterService paras = ParamService.getInstance();
        String path = paras.getParameter("config.file.luncene_index_dir");
        try {
            if (path.startsWith("WEBROOT")) {
                path = path.replace("WEBROOT",
                        paras.getParameter("farm.constant.webroot.path "));
            }
            String separator = File.separator;
            if (separator.equals("\\")) {
                separator = "\\\\";
            }
            path = path.replaceAll("\\\\", "/").replaceAll("/", separator);
        } catch (Exception e) {
            log.warn(path + ":路径地址有误！");
            path = paras.getParameter("config.doc.dir");
        }
        return path;
    }
}
